/**
 * 用户相关API
 */

import { http } from '@/utils/request'
import type { VoicePreferences } from '@/types/chat'

// 用户个人资料相关API
export interface UserProfile {
  id: number
  username: string
  email: string
  phone: string
  avatarUrl?: string
  gender: number
  age?: number
  status: number
  lastLoginTime?: string
  createdAt: string
  loginCount: number
  totalConversations: number
}

export interface UserProfileUpdateDTO {
  username?: string
  email?: string
  phone?: string
  gender?: number
  age?: number
}

export interface PasswordUpdateDTO {
  oldPassword: string
  newPassword: string
  confirmPassword: string
}

export interface VoicePreferencesUpdateDTO {
  preferredCharacterType?: string
  voiceSpeed?: number
  voicePitch?: number
  voiceTone?: string
  voiceLanguage?: string
  autoEmotionDetection?: boolean
  privacyLevel?: number
  notificationEnabled?: boolean
  voiceNotificationEnabled?: boolean
}

// 获取用户详细信息
export const getUserProfile = () => 
  http.get<UserProfile>('/user/profile')

// 更新用户基本信息
export const updateUserProfile = (data: UserProfileUpdateDTO) => 
  http.put<UserProfile>('/user/profile', data)

// 上传用户头像
export const uploadUserAvatar = (file: File) => {
  const formData = new FormData()
  formData.append('avatarFile', file)
  return http.post<{ 
    code: number; 
    message: string; 
    data: string; 
    timestamp: number 
  }>('/user/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 修改密码
export const updatePassword = (data: PasswordUpdateDTO) => 
  http.put<string>('/user/password', data)

// 获取登录记录
export const getLoginHistory = (page: number = 1, size: number = 10) => 
  http.get<{
    total: number
    page: number
    size: number
    records: Array<{
      id: number
      loginTime: string
      ipAddress: string
      deviceType: string
      browser: string
      os: string
      location?: string
      status: number
      sessionDuration?: number
    }>
  }>(`/user/login-history?page=${page}&size=${size}`)

// 语音设置相关API
// 获取语音设置
export const getVoicePreferences = (userId: number) => 
  http.get<VoicePreferences>(`/user/voice-preferences?userId=${userId}`)

// 更新语音设置
export const updateVoicePreferences = (userId: number, preferences: VoicePreferencesUpdateDTO) => 
  http.put<VoicePreferences>(`/user/voice-preferences?userId=${userId}`, preferences)

// 重置语音设置为默认值
export const resetVoicePreferences = (userId: number) => 
  http.post<VoicePreferences>(`/user/voice-preferences/reset?userId=${userId}`)

